package com.trickytriads.viewmodels
{
	import com.trickytriads.events.ChangeMainViewStateEvent;
	import com.trickytriads.events.GetAllCardsEvent;
	import com.trickytriads.events.LoadingEvent;
	import com.trickytriads.services.interfaces.I_AssetService;
	import com.trickytriads.viewmodels.interfaces.I_MainViewModel;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/**
	 * Viewmodel for MainView
	 **/
	 // Initialize asynchronously
	[AsyncInit]
	[Event(name="changeMainView", type="com.trickytriads.events.ChangeMainViewStateEvent")]
	[Event(name="loadGeneric", type="com.trickytriads.events.LoadingEvent")]
	[Event(name="getAllCardsRequest", type="com.trickytriads.events.GetAllCardsEvent")]	
	[ManagedEvents("changeMainView, loadGeneric, getAllCardsRequest")]
	public class MainViewModel extends EventDispatcher implements I_MainViewModel
	{
				
		// Name of the background image for the main menu
		private var _backgroundImageName:String = "mainmenu.jpg";
		private var _newsButtonImageName:String = "menu/news.jpg";
		private var _gameButtonImageName:String = "menu/game.jpg";
		private var _storeButtonImageName:String = "menu/store.jpg";
		
		// Viewstack Index
		private var _viewStackIndex:int = 0;
		
		// Asset service
		[Inject]
		public var assetService:I_AssetService;		
		
		// Click button (dispatch an event to change the view)
		public function changeView (i:int) : void
		{
			dispatchEvent(new ChangeMainViewStateEvent(ChangeMainViewStateEvent.CHANGE_MAIN_VIEW, i));
		}
		
		/**
		 * Retrieves the card data from the AssetService.
		 */
		[PostConstruct]
		public function retrieveCardData () : void
		{
			// trace("Retrieving card data");
			// dispatch a generic loading event
			// @TODO doesn't display for now?
			dispatchEvent(new LoadingEvent(LoadingEvent.LOAD_GENERIC, "Retrieving card data..."));
			// dispatch the actual CardData retrieval event
			dispatchEvent(new GetAllCardsEvent(GetAllCardsEvent.GETALLCARDS_REQUEST));
		}
		
		/**
		 * Simple function that notifies Parsley that the card loading is complete.
		 */
		[MessageHandler(selector="getAllCardsDone")]
		public function cardDataFinished() : void
		{
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		[Bindable]
		public function get backgroundImageName() : String { return assetService.getImageURL(_backgroundImageName); }
		public function set backgroundImageName(v:String) : void { _backgroundImageName = v; }
		
		[Bindable]
		public function get newsButtonImageName() : String { return assetService.getImageURL(_newsButtonImageName); }
		public function set newsButtonImageName(v:String) : void { _newsButtonImageName = v; }
		
		[Bindable]			
		public function get gameButtonImageName() : String { return assetService.getImageURL(_gameButtonImageName); }
		public function set gameButtonImageName(v:String) : void { _gameButtonImageName = v; }
						
		[Bindable]
		public function get storeButtonImageName() : String { return assetService.getImageURL(_storeButtonImageName); }				
		public function set storeButtonImageName(v:String) : void { _storeButtonImageName = v; }

	}
}